home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / metaphn.com / METAFILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-11  |  3.6 KB  |  134 lines

  1. /*-------------------- METAFILE.C --------------------------
  2. *
  3. * Author: Gary Parker
  4. * Last update: 3-12-91
  5. *
  6. * Modified by: Al Watters
  7. * Date: 6-11-91
  8. * Modifications:
  9. *   line changed from : #if defined(_TURBOC_)   Turbo C
  10. *                  to : #if defined(__BORLANDC__)    Borland C
  11. *   compiles and runs OK under Borland C++ 2.0
  12. *
  13. * Compilers: Borland C++ 2.0, Turbo C++, Turbo C 2.0, MSC 6.0
  14. * Memory model: any
  15. *
  16. * A demonstration program for metaphone().
  17. * The program will search a drive for a filename
  18. * which sounds similar to the name passed.
  19. *
  20. * This code is hereby placed in the public domain.
  21. *-----------------------------------------------------------------*/
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <dos.h>
  27.  
  28. #if defined(__BORLANDC__)   /* Borland C */
  29.     #include <dir.h>
  30. typedef struct ffblk FILEINFO;
  31.     #define F_FIRST(a,b,c) findfirst(a,c,b)
  32.     #define F_NEXT         findnext
  33.     #define F_ATTRIB       ff_attrib
  34.     #define F_DATE         ff_fdate
  35.     #define F_SIZE         ff_fsize
  36.     #define F_NAME         ff_name
  37. #else
  38. typedef struct find_t  FILEINFO;  /* MSC */
  39.     #define F_FIRST        _dos_findfirst
  40.     #define F_NEXT         _dos_findnext
  41.     #define F_ATTRIB       attrib
  42.     #define F_DATE         wr_fdate
  43.     #define F_SIZE         size
  44.     #define F_NAME         name
  45. #endif
  46.  
  47. /* file attributes */
  48. #define RDONLY   0x01 /* Read only file */
  49. #define HIDDEN   0x02 /* Hidden file */
  50. #define SYSTEM   0x04 /* System file */
  51. #define SUBDIR   0x10 /* Subdirectory */
  52. #define ARCH     0x20 /* Archive file */
  53.  
  54. /* display the file and path if it matches */
  55. void print_find_t(char *dir, FILEINFO *find);
  56.  
  57. /* perform metaphone comparison */
  58. int metaphone(char *name, char *m, int first);
  59.  
  60. /* search dos file system recursively */
  61. void search(char *dir, char *name);
  62.  
  63. int found = 0;
  64. char meta[8]; /* storage for first metaph */
  65.  
  66. main(int argc, char *argv[])
  67. {
  68.     char *ptr;
  69.  
  70.     if(argc == 1) {
  71.         puts("syntax METAFILE [filename.ext]");
  72.         exit(1);
  73.     }
  74.     /* remove extension if any */
  75.     if((ptr = memchr(argv[1], '.', 9)) != 0) *ptr = 0;
  76.     metaphone(argv[1], meta, 1); /* store the metaph */
  77.     printf("\nmetaph for %s is %s\n", argv[1], meta);
  78.     search("", "");              /* search for matchs */
  79.     if(!found) printf("A match was not found\n");
  80.     return 0;
  81. }
  82.  
  83. void search(char *dir, char *newdir)
  84. {
  85.     char curdir[80];
  86.     FILEINFO find;
  87.     char *ptr;
  88.  
  89.     strcpy(curdir, dir);  /* build first pathname */
  90.     strcat(curdir, newdir);
  91.     strcat(curdir, "\\*.*");
  92.  
  93.     /* check first file */
  94.     if( !F_FIRST( curdir, 0xFF, &find)) {
  95.         curdir[strlen(curdir) - 3] = 0; /* remove *.* */
  96.         do {
  97.             if(find.F_NAME[0] == '.') continue;
  98.             else {                        /* skip the extension */
  99.                 if((ptr = memchr(find.F_NAME, '.', 9)) != 0)
  100.                     *ptr = 0;
  101.  
  102.                     /* call metaphone with flag set to
  103.                          0 to compare string stored metaph */
  104.  
  105.                     if(metaphone(find.F_NAME, meta, 0) == 0) {
  106.                         /* put back extension */
  107.                         if(ptr != 0) *ptr = '.';
  108.                         print_find_t(curdir, &find);
  109.                     }
  110.                 }
  111.  
  112.                 if((find.F_ATTRIB & SUBDIR))
  113.                     search(curdir, find.F_NAME); /* recursion */
  114.         } while( !F_NEXT( &find));
  115.     }
  116. }
  117. /* display the file information */
  118. void print_find_t(char *dir, FILEINFO *find)
  119. {
  120.     printf("%s%-12s %8ld %2.2d-%02.2d-%2.2d %c%c%c%c%c\r\n",
  121.         dir, find->F_NAME, find->F_SIZE,
  122.         (find->F_DATE >> 5) & 0x0f,
  123.         find->F_DATE & 0x1f,
  124.         ((find->F_DATE >> 9) + 80) % 100,
  125.         (find->F_ATTRIB & SUBDIR) ? 'D' : '.',
  126.         (find->F_ATTRIB & RDONLY) ? 'R' : '.',
  127.         (find->F_ATTRIB & HIDDEN) ? 'H' : '.',
  128.         (find->F_ATTRIB & SYSTEM) ? 'S' : '.',
  129.         (find->F_ATTRIB & ARCH)   ? 'A' : '.');
  130.  
  131.     found = 1;
  132. }
  133.  
  134.